package org.top10.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.top10.collector.Collector;
import org.top10.entity.Blog;
import org.top10.entity.CollectEntity;
import org.top10.entity.Ent;
import org.top10.entity.Finance;
import org.top10.entity.Forum;
import org.top10.entity.News;
import org.top10.entity.Pic;
import org.top10.entity.Reading;
import org.top10.entity.Sport;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


public class QQTask extends Task {
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_QQ;// 对应网站ID
	private static final String ENCoding = "gb2312";// 对应网站ID

	public QQTask() {
		super(webSiteID, ENCoding);
	}

	@Override
	public void loadParser() {
		PARSER.add(new QQChineseParser());
		PARSER.add(new QQAbroadParserr());
		PARSER.add(new QQCommunityParserr());
		PARSER.add(new QQEntertainmentParser());
		PARSER.add(new QQFinanceParserr());
		PARSER.add(new QQSportsParserr());
		PARSER.add(new QQForumParserr());
		PARSER.add(new QQPICParserr());
		PARSER.add(new QQBlogParserr());
		PARSER.add(new QQBookParserr());
	}

	public static void main(String arg[]) throws Exception {
		new QQBookParserr().dealText(ENCoding);
	}

}

abstract class QQParser extends TOPParserImpl {
	public abstract List parse(ParseSource doc) throws Exception;

	public abstract CollectEntity build(Node node, int rank) throws Exception;

	public QQParser(String c, int type, String u) {
		super(c, type, u);
	}

	public List parse1(ParseSource source, String sign) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Pattern p = Pattern.compile(sign, Pattern.MULTILINE);
		Node node = getNodeByPattern(body, "td", p);
		node = node.getParentNode().getParentNode().getParentNode()
				.getNextSibling().getNextSibling().getNextSibling()
				.getNextSibling();
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			for (Node n : dls) {
				CollectEntity v = build(n, rank);
				if (v == null)
					continue;
				rank++;
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}

	public void build1(Node node, int rank, CollectEntity v) {
		v.setRank(rank);
		v.setLinkUrl(getAttrValueByTag(node, "href"));
		v.setName(node.getTextContent());
	}
}

class QQChineseParser extends QQParser {
	private final static String SIGN = "国内新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = SIGN;

	public QQChineseParser() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public News build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		News n = new News();
		build1(node, rank, n);
		return n;
	}

}

class QQAbroadParserr extends QQParser {
	private final static String SIGN = "国际新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = SIGN;

	public QQAbroadParserr() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public News build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		News n = new News();
		build1(node, rank, n);
		return n;
	}

}

class QQEntertainmentParser extends QQParser {
	private final static String SIGN = "娱乐新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = "娱乐";

	public QQEntertainmentParser() {
		super(TITLE, TOP10Global.TYPE_ENT, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public Ent build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		Ent n = new Ent();
		build1(node, rank, n);
		return n;
	}

}

class QQFinanceParserr extends QQParser {
	private final static String SIGN = "财经新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = "财经";

	public QQFinanceParserr() {
		super(TITLE, TOP10Global.TYPE_Finance, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public Finance build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		Finance n = new Finance();
		build1(node, rank, n);
		return n;
	}

}

class QQCommunityParserr extends QQParser {
	private final static String SIGN = "社会新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = "社会";

	public QQCommunityParserr() {
		super(TITLE, TOP10Global.TYPE_News, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public News build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		News n = new News();
		build1(node, rank, n);
		return n;
	}

}

class QQSportsParserr extends QQParser {
	private final static String SIGN = "体育新闻";
	private static final String URL = "http://news.qq.com/paihang.htm";
	private static final String TITLE = "体育";

	public QQSportsParserr() {
		super(TITLE, TOP10Global.TYPE_Sports, URL);
	}

	public List parse(ParseSource source) throws Exception {
		return parse1(source, SIGN);
	}

	public Sport build(Node node, int rank) throws Exception {
		String classVal = getAttrValueByTag(node, "class");
		if (!classVal.equals("lcu333"))
			return null;
		Sport n = new Sport();
		build1(node, rank, n);
		return n;
	}
}

class QQForumParserr extends QQParser {
	private final static String SIGN = "今日精华贴排行";
	private static final String URL = "http://bbs.qq.com/";
	private static final String TITLE = "今日精华贴";

	public QQForumParserr() {
		super(TITLE, TOP10Global.TYPE_Forum, URL);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"TD",SIGN);
		node = node.getParentNode().getParentNode().getParentNode();
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			for (Node n : dls) {
				CollectEntity v = build(n, rank);
				rank++;
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}

	public Forum build(Node node, int rank) throws Exception {
		Forum n = new Forum();
		build1(node, rank, n);
		return n;
	}
}

class QQPICParserr extends QQParser {
	private final static String SIGN = "强图打榜";
	private static final String URL = "http://news.qq.com/photo.shtml";
	private static final String TITLE = SIGN;

	public QQPICParserr() {
		super(TITLE, TOP10Global.TYPE_PIC, URL);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeContentText(body,"b",SIGN).getParentNode().getParentNode().getParentNode().getParentNode();
		if (node != null) {
			node = node.getNextSibling().getNextSibling();
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "a", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			for (Node n : dls) {
				CollectEntity v = build(n, rank);
				rank++;
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
	public Pic build(Node node, int rank) throws Exception {
		Pic n = new Pic();
		build1(node, rank, n);
		return n;
	}
}
class QQBlogParserr extends QQParser {
	private final static String SIGN = "link6";
	private static final String URL = "http://blog.qq.com/index/paihang_day.htm";
	private static final String TITLE = "博客排行";

	public QQBlogParserr() {
		super(TITLE, TOP10Global.TYPE_Blog, URL);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("table","id",SIGN,body);
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "tr", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			while (rank<11) {
				CollectEntity v = build(dls.get(rank-1), rank);
				rank++;
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
	public Blog build(Node node, int rank) throws Exception {
		Blog b = new Blog();
		b.setRank(rank);
		ArrayList<Node> dls = new ArrayList<Node>();
		getAllTagsByParent(node, "a", dls);
		Node a=dls.get(0);
		b.setLinkUrl(getAttrValueByTag(a, "href"));
		b.setName(a.getTextContent());
		return b;
	}
}
class QQBookParserr extends QQParser {
	private final static String SIGN = "VIP图书总排行";
	private static final String URL = "http://book.qq.com/rankcount/lz_ph_vip_click_day.htm";
	private static final String TITLE = "图书排行";

	public QQBookParserr() {
		super(TITLE, TOP10Global.TYPE_Reading, URL);
	}

	public List parse(ParseSource source) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeContentText(body,"table",SIGN);
		if (node != null) {
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(node, "tr", dls);
			List<CollectEntity> list = new ArrayList<CollectEntity>();
			int rank = 1;
			while (rank<11) {
				CollectEntity v = build(dls.get(rank+2), rank);
				rank++;
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
	public Reading build(Node node, int rank) throws Exception {
		Reading b = new Reading();
		b.setRank(rank);
		ArrayList<Node> dls = new ArrayList<Node>();
		getAllTagsByParent(node, "a", dls);
		Node a=dls.get(0);
		b.setLinkUrl(getAttrValueByTag(a, "href"));
		b.setName(a.getTextContent());
		return b;
	}
}